# common target a
# Created by tongzhilin on 2020.08.05.

#字体颜色 重置=0，黑色=30，红色=31，绿色=32， 黄色=33，蓝色=34， 洋红（粉）=35， 青色=36， 白色=37
#背景颜色 重置=0，黑色=40，红色=41，绿色=42， 黄色=43，蓝色=44， 洋红（粉）=45， 青色=46， 白色=47

SRC  := $(foreach dir, $(SUB_DIRS), $(wildcard $(dir)/*.c))
OBJ  := $(SRC:%.c=%.o)
DEPS := $(SRC:%.c=%.o.d)

CPPSRC  := $(foreach dir, $(SUB_DIRS), $(wildcard $(dir)/*.cpp))
CPPOBJ  := $(CPPSRC:%.cpp=%.o)
CPPDEPS := $(CPPSRC:%.cpp=%.o.d)
DEPS    += $(CPPDEPS)

CXXFLAGS += $(INC_FLAGS)
CFLAGS += $(INC_FLAGS)

FILE_NONE = 
ifneq ($(TARGET_DEPEND_A), $(FILE_NONE))
  ifeq ($(TARGET_DEPEND_A), $(wildcard $(TARGET_DEPEND_A)))
	DEPEND_A_OBJS = ./*.o
	TARGET_DEPEND_A_EXIST=y
  endif
endif

all: $(TARGET)

$(TARGET):$(OBJ) $(CPPOBJ)
	@echo -e "\e[1;36mTARGET AR $@ start \e[0m "
  ifeq ($(TARGET_DEPEND_A_EXIST), y)
	$(PR)for i in $(TARGET_DEPEND_A);do $(AR) -x $$i;done
  endif
	$(PR)$(AR) $(ARFLAGS) $@ $^ $(DEPEND_A_OBJS)
  ifeq ($(TARGET_DEPEND_A_EXIST), y)
	$(PR)rm -f $(DEPEND_A_OBJS)
  endif
	@echo -e "\e[1;36mTARGET AR $@ success \e[0m "

$(OBJ): $ %.o: %.c
	$(PR)$(CC) -c $< -o $@ $(CFLAGS) -MD -MF "$(dir $@)$(notdir $@).d" -MT "$@"
	@echo CC $@ 

$(CPPOBJ): $ %.o: %.cpp
	$(PR)$(CPP) -c $< -o $@ $(CXXFLAGS) -MD -MF "$(dir $@)$(notdir $@).d" -MT "$@"
	@echo CXX $@ 
	
clean:
	$(PR)rm -f $(TARGET)
	$(PR)rm -f $(OBJ) $(CPPOBJ) $(DEPS)

.PHONY : clean all

-include $(DEPS)
